package com.ice_col.viewrain.myview;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2015/9/9 0009.
 */
public abstract class BaseView extends View{
    Thread mThread;
    public BaseView(Context context) {
        super(context);
    }

    public BaseView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BaseView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected final void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        myOnDraw(canvas);
        if(mThread==null){
            mThread=new MyThread();
            mThread.start();
        }
    }

    protected abstract void myOnDraw(Canvas canvas);
    protected abstract void reDraw();
    private boolean isShow=true;

    @Override
    protected void onDetachedFromWindow() {
        isShow=false;
        super.onDetachedFromWindow();
    }

    class MyThread extends Thread{
        @Override
        public void run() {
           while(isShow){
               reDraw();
               postInvalidate();
               try {
                   Thread.sleep(30);
               }catch (Exception e){
                   e.printStackTrace();
               }
           }
        }
    }
}
